﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
using System.IO;

namespace CustomTypes.Mono
{


    public class CustomAssemblyResolver : DefaultAssemblyResolver
    {


        public override AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            return base.Resolve(name);
        }

        public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {

            string fullname = SearchDirectory(name, this.GetSearchDirectories());

            AssemblyDefinition ass = base.Resolve(name, parameters);
            
            base.RegisterAssembly(ass);

            return ass;

        }


        string SearchDirectory(AssemblyNameReference name, IEnumerable<string> directories)
        {
            var extensions = new[] { ".dll", ".exe" };

            foreach (var extension in extensions)
            {
                foreach (var directory in directories)
                {
                    string file = Path.Combine(directory, name.Name + extension);
                    if (File.Exists(file))
                        return file;
                }
            }

            return null;
        }


        public override AssemblyDefinition Resolve(string fullName)
        {
            return base.Resolve(fullName);
        }


        
    }


}
